﻿using System;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Xml;
using System.IO;
using PSGQuickTimeTracker.Properties;



namespace PSGQuickTimeTracker
{
	public class Update
	{
		static string pubXml = Settings.Default.OneClickAppPath;
		

		/// <summary>
		/// Returns a boolean value representing whether or not an update is available.
		/// </summary>
		public static bool CheckUpdate()
		{
			if (new FileInfo(pubXml).Exists)
				return !(Application.ProductVersion == Version.GetPublishVersion());
			else
				return false;
		}
	}



	/// <summary>
	/// Wrapper for basic registry functions.
	/// </summary>
	public class Version
	{
		static string BaseKey = "HKEY_CURRENT_USER\\Software";
		static string appName = Application.ProductName;


		/// <summary>
		/// Returns the value of the 'Version' string within the given registry key.
		/// If the key/string value isn't found, null is returned. Note that it is
		/// assumed that BaseKey property is set and correct.
		/// </summary>
		public static string GetRegVersion()
		{
			return (string)Registry.GetValue(BaseKey + "\\" + appName, "Version", null);
		}

		/// <summary>
		/// Gets the version of the latest published version from the OneClick application file
		/// </summary>
		public static string GetPublishVersion()
		{
			XmlDocument xdoc = new XmlDocument();
			xdoc.Load(@"\\corp.halliburton.com\team\WD\Business Development and Technology\General\Engineering Public\PSG Apps\QuickTaskTracker\PSGQuickTimeTracker.application");
			return xdoc.ChildNodes[1].FirstChild.Attributes["version"].Value.ToString();
		}


		/// <summary>
		/// Sets the version in the registry.
		/// </summary>
		public static void SetRegVersion(string version)
		{
			try
			{
				Registry.SetValue(BaseKey + "\\" + appName, "Version", version);
			}
			catch (Exception e)
			{
				throw e;
			}
		}
	}
}
